﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace HelpPoj.MoveFilesToFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            int spanMinutes = 0;
            if (args.Length > 0)
            {
                spanMinutes = Int32.Parse(args[0]);
            }
            string curentFolder = AppDomain.CurrentDomain.BaseDirectory;
            Directory.GetDirectories(curentFolder).ToList().ForEach(folder =>
            {
                string folderName = folder.Substring(folder.LastIndexOf("\\") + 1);
                string[] files = System.IO.Directory.GetFiles(curentFolder);
                string srcFile = null;  //added by Linson at 2011-03-24
                if (files.Length > 0)
                {
                    files.Where(f => f.Substring(f.LastIndexOf("\\") + 1).StartsWith(folderName))
                        .ToList().ForEach((file) =>
                    {
                        //added by Linson at 2011-03-24 begin
                        srcFile = file.Substring(file.LastIndexOf("\\") + 1);                        
                        if (File.Exists(folder + "\\" + srcFile))
                        {
                            FileInfo dstFile=new FileInfo(folder + "\\" + file.Substring(file.LastIndexOf("\\") + 1));
                            File.Move(folder + "\\" + srcFile, folder + "\\" + srcFile.Insert(srcFile.LastIndexOf('.'), "_" + dstFile.CreationTime.ToString("HHmmss")));
                        }
                        //added by Linson at 2011-03-24 end
                        System.IO.File.Move(file, folder + "\\" + file.Substring(file.LastIndexOf("\\") + 1));
                    });
                    System.Threading.Thread.Sleep(new TimeSpan(0, spanMinutes, 0));
                }
            });

        }
    }
}
